import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { AppModule } from './app.module';
import { LogMiddleware } from './middlewares/log.middleware'
import { TransformInterceptor } from 'src/interceptors/transform.interceptor'
// import {AuthGuard} from './guard/auth.guard'
// import {ValidationPipe} from '@nestjs/common'
import { HttpExceptionFilter } from 'src/filters/http-exception.filter'

const PORT = process.env.PORT || 5000
const PREFIX = process.env.PREFIX || 'api/v1'

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  //全局中间件
  app.use(new LogMiddleware().use)
  //全局守卫
  // app.useGlobalGuards(new AuthGuard())
  //全局管道
  // app.useGlobalPipes(new ValidationPipe())
  //全局拦截器
  app.useGlobalInterceptors(new TransformInterceptor())
  //全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter())

  //=======文档=======
  // 给请求添加prefix
  app.setGlobalPrefix(PREFIX);
  const options = new DocumentBuilder()
    .setTitle('nest framework  api文档')
    .setDescription('nest framework  api接口文档')
    .setBasePath(PREFIX) // 设置基础的路径
    .addBearerAuth({ type: 'apiKey', in: 'header', name: 'token' }) // 设置请求头的token字段
    .setVersion('0.0.1')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup(`${PREFIX}/docs`, app, document);


  await app.listen(PORT);
}
bootstrap();
